﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using POI.eTransmittal.Enitity;

using System.Data;
using System.IO;

namespace POI.eTransmittal.WebApp
{
    public partial class Profile : BasePage
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            RegisterXpodatasource(xpodsRoles, xpodsTransmittalUser, xpodsUserList);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = AccessRights.GetList();

            grdAccessRights.DataSource = dt;
            grdAccessRights.DataMember = dt.TableName;

            grdAccessRights.DataBind();
        }

        protected void grdRoles_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void grdRoles_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void grdRoles_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        protected void grdAccessRights_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            DataTable dt = AccessRights.GetList();

            grdAccessRights.DataSource = dt;
            grdAccessRights.DataMember = dt.TableName;
            //grdAccessRights.KeyFieldName = "AccessRight";

            grdAccessRights.DataBind();

        }

        protected void grdUsers_BeforePerformDataSelect(object sender, EventArgs e)
        {
            DevExpress.Web.ASPxGridView.ASPxGridView grdUser = (DevExpress.Web.ASPxGridView.ASPxGridView)sender;

            string roleID = grdUser.GetMasterRowKeyValue().ToString();

            xpodsTransmittalUser.Criteria = CriteriaOperator.Parse("Role.ID=?", roleID).ToString();

            //grdUser.DataBind();
        }

        protected void grdUsers_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
        {
            (sender as DevExpress.Web.ASPxGridView.ASPxGridView).CancelEdit();
        }

        protected void grdUsers_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            Role role = CurrentUnitOfWork.FindObject<Role>(CriteriaOperator.Parse("ID=?", (sender as DevExpress.Web.ASPxGridView.ASPxGridView).GetMasterRowKeyValue()));

            string selectedUsers = txtUserListSelectedValues.Text;

            string[] arrSelectedUsers = selectedUsers.Split(',');
            foreach (string selectedUser in arrSelectedUsers)
            {
                User user = new User(CurrentUnitOfWork);

                
                TransmittalUser poUser = CurrentUnitOfWork.FindObject<TransmittalUser>(CriteriaOperator.Parse("UserID=?", selectedUser));

                user.UserID = selectedUser;
                user.Role = role;
                user.PremierOilUser = poUser;

                user.Save(CurrUserId);
            }

            CurrentUnitOfWork.CommitChanges();

            (sender as DevExpress.Web.ASPxGridView.ASPxGridView).DataBind();
        }

        protected void grdUsers_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {

        }

        protected void grdUsers_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e)
        {
            CurrentUnitOfWork.CommitChanges();
        }

        
    }
}